import { assign } from 'lodash'
import { fetchTableData, fetchMeterDetail, fetchEBoxline, fetchELine, fetchCalendar } from '@/api/leakMonitoring'

const defaultState = () => {
  return {
    defaultDate: undefined,
    tableData: null,
    tableLoading: false,
    tableHint: '',
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    currentMeterId: undefined,
    meterDetail: null,
    userQueryParams: {
      unitId: undefined,
      unitName: undefined
    },
    stopTableKeyUp: false,
    treeMapList: null,
    collapseLeftFlag: false,
    collapseRightFlag: false,
    centerTopChartLoading: false,
    centerBottomChartLoading: false,
    rightTopChartLoading: false,
    eBoxlineData: null,
    eLineData: null,
    CalendarList: []
  }
}

export default {
  namespaced: true,
  name: 'leakMonitoring',
  state: defaultState(),
  getters: {
    getTablePageParams: state => {
      return {
        ...state.tablePageParams
      }
    }
  },
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    SET_CURRENT_METERID(state, meterId) {
      state.currentMeterId = meterId
    },
    /**
     * desc: 设置表格数据
     * param {type}
     * return {type} void
     */
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    /**
     * desc: 设置表格加载状态
     * param {type}
     * return {type} void
     */
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    /**
     * desc: 设置表格请求页数
     * param {type}
     * return {type} void
     */
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    /**
     * desc: 设置表格键盘操作标识符
     * param {type}
     * return {type} void
     */
    SET_STOP_TABLE_KEYUP(state, flag) {
      state.stopTableKeyUp = flag
    },
    SET_COLLAPSE(state, { type, status }) {
      if (type === 'left') {
        state.collapseLeftFlag = status
      } else if (type === 'right') {
        state.collapseRightFlag = status
      }
    },
    SET_METER_DETAIL(state, data) {
      state.meterDetail = data
    },
    SET_CENTER_TOP_CHART_LOADING(state, flag) {
      state.centerTopChartLoading = flag
    },
    SET_CENTER_BOTTOM_CHART_LOADING(state, flag) {
      state.centerBottomChartLoading = flag
    },
    SET_RIGHT_TOP_CHART_LOADING(state, flag) {
      state.rightTopChartLoading = flag
    },
    SET_EBOXLINE_DATA(state, data) {
      state.eBoxlineData = data
    },
    SET_ELINE_DATA(state, data) {
      state.eLineData = data
    },
    SET_CALENDAR_LIST(state, list) {
      state.CalendarList = list
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    /**
     * desc: 获取表格数据
     * param {type}
     * return {type} void
     */
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      const _tableData = await fetchTableData({
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      const { code, date = {} } = _tableData
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    async lodaMeterDetail(ctx) {
      const { code, date } = await fetchMeterDetail(ctx.state.currentMeterId)
      if (code === 200) {
        ctx.commit('SET_METER_DETAIL', date)
      }
    },
    /**
     * desc: 修改表格页码
     * param {type}
     * return {type} void
     */
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    /**
     * desc: 是否允许表格键盘操作
     * param {type}
     * return {type} void
     */
    changeStopTableKeyUp(ctx, flag) {
      ctx.commit('SET_STOP_TABLE_KEYUP', flag)
    },
    changeCurrentMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METERID', meterId)
    },
    changeCollapse(ctx, arg) {
      ctx.commit('SET_COLLAPSE', arg)
    },
    async loadEBoxline(ctx, params) {
      ctx.commit('SET_CENTER_TOP_CHART_LOADING', true)
      const { code, date } = await fetchEBoxline({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      ctx.commit('SET_CENTER_TOP_CHART_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_EBOXLINE_DATA', date)
      }
    },
    async loadELine(ctx, params) {
      ctx.commit('SET_CENTER_BOTTOM_CHART_LOADING', true)
      const { code, date } = await fetchELine({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      ctx.commit('SET_CENTER_BOTTOM_CHART_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_ELINE_DATA', date)
      }
    },
    async loadCalendar(ctx, params) {
      ctx.commit('SET_RIGHT_TOP_CHART_LOADING', true)
      const { code, date } = await fetchCalendar({
        meterId: ctx.state.currentMeterId,
        ...params
      })
      ctx.commit('SET_RIGHT_TOP_CHART_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_CALENDAR_LIST', date)
      }
    }
  }
}
